package com.wealth.bbs.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wealth.bbs.entity.Topic;
import com.wealth.bbs.enums.TopicAppraiseEnum;
import com.wealth.bbs.service.ITopicService;
import com.wealth.bbs.vo.TopicVO;
import com.wealth.member.entity.Member;
import com.wealth.member.service.IMemberService;
import com.wealth.system.web.controller.BaseController;

import iaas.annotation.Login;
import iaas.utils.ResultObject;

/**
 * 论坛
 * @author chan
 *
 */
@Controller
@RequestMapping(value = "/bbs/topic")
public class TopicController extends BaseController {
	
	Logger logger = Logger.getLogger(TopicController.class);
	
	@Autowired
	private IMemberService memberService;
	@Autowired
	private ITopicService topicService;
	
	@ModelAttribute
	public void init(Model model){
		
	}
	
	/** 
	 * 主题加载
	 */
	@RequestMapping(value="/more",method = RequestMethod.POST)
	@ResponseBody
	public ResultObject topicAjax(
			@RequestParam(defaultValue="1L") Long boardid,
			@RequestParam(defaultValue="1") Integer isRecommend,
			@RequestParam(defaultValue="-1") Integer index,
			@RequestParam(defaultValue="10") Integer offset) {
		try {
			List<TopicVO> volist = topicService.ajaxTopicVO(boardid, memberid, isRecommend, index, offset);
			return new ResultObject("000","主题加载更多成功", volist);
		} catch (Exception e) {
			return new ResultObject("999","主题加载更多错误", e.getMessage());
		}
	}
	
	/**
	 * 详情
	 */
	@RequestMapping(value = "/{topicid}")
	@Login
	public String info(Model model,@PathVariable("topicid") Long topicid) {
		try{
			/** --------业务处理 -------- */
			Member member = memberService.get(memberid);
			model.addAttribute("member", member);
			
			TopicVO topicvo = topicService.getTopicVO(topicid, memberid);
			model.addAttribute("topicvo", topicvo);

			return "bbs/topic/info";
		}catch(Exception e){
			return "system/500";
		}	
	}
	
	/**
	 * 新增页面
	 */
	@RequestMapping(value = "/create")
	@Login
	public String create(Model model, Long boardid) {
		try{
			model.addAttribute("boardid", boardid);
			return "bbs/topic/create";
		}catch(Exception e){
			return "system/500";
		}	
	}
	
	/** 
	 * 新增
	 */
	@RequestMapping(value="/create",method = RequestMethod.POST)
	@ResponseBody
	public ResultObject topicCreate(Topic topic,String picurls,HttpServletRequest request){
		try {
			String picList[]=request.getParameterValues("picList[]");
			return topicService.saveTopic(topic, picList,memberid);
		} catch (Exception e) {
			return new ResultObject("999","主题新增错误", e.getMessage());
		}
	}
	
	/** 
	 * 主题点赞/取消点赞
	 */
	@RequestMapping(value="/{topicid}/support/{status}",method = RequestMethod.POST)
	@ResponseBody
	public ResultObject topicSupport(@PathVariable Long topicid,@PathVariable Integer status,
			Long memberid,HttpServletRequest request) {
		try {
			return topicService.appraiseTopic(topicid, memberid, TopicAppraiseEnum.SUPPORT.getValue(), status);
		} catch (Exception e) {
			return new ResultObject("999","主题点赞/取消点赞错误", e.getMessage());
		}
	}
}
